<template>
  <header @mousedown="startDrag" @mousemove="onDrag" @mouseup="stopDrag" @mouseleave="stopDrag">
    <div class="title">大白 AI 机器人</div>
    <div class="option">
      <div @click="toTop" :class="isTop ? 'bg' : ''"><icon-to-top /></div>
      <div @click="minWindow"><icon-minus /></div>
      <div @click="closeWindow"><icon-close /></div>
    </div>
  </header>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const isDragging = ref(false)
const dragOffset = ref({ x: 0, y: 0 })
const isTop = ref(false)

const startDrag = (e: MouseEvent) => {
  isDragging.value = true
  dragOffset.value = {
    x: e.clientX,
    y: e.clientY
  }
}

const onDrag = (e: MouseEvent) => {
  if (!isDragging.value) return
  let x = e.screenX - dragOffset.value.x,
    y = e.screenY - dragOffset.value.y
  window.electron.ipcRenderer.send('dragWindow', { x, y })
}

const stopDrag = () => {
  isDragging.value = false
}

const minWindow = () => {
  window.electron.ipcRenderer.send('minWindow')
}

const closeWindow = () => {
  window.electron.ipcRenderer.send('closeWindow')
}

const toTop = () => {
  window.electron.ipcRenderer.send('toTop')
  isTop.value = !isTop.value
}
</script>
<style lang="scss" scoped>
header {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  background-color: #2f5fd0;
  user-select: none;
  .title {
    color: #ffffff;
  }
  .option {
    display: flex;
    align-items: center;
    justify-content: space-between;
    > div {
      width: 24px;
      height: 24px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #ffffff;
      cursor: pointer;
      &:hover {
        background-color: rgba(255, 255, 255, 0.15);
      }
      &:active {
        background-color: rgba(255, 255, 255, 0.25);
      }
      &:nth-of-type(1) {
        margin-right: 4px;
      }
    }
    .bg {
      background-color: rgba(255, 255, 255, 0.15);
    }
  }
}
</style>
